package net.bwie.flink;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

/**
 * 调用HBase Java API，从表中读取数据
 * @author xuanyu
 * @date 2025/10/30
 */
public class _02HbaseGetDemo {

	public static void main(String[] args) throws Exception{
		// 1.连接
		Configuration conf = HBaseConfiguration.create();
		conf.set("hbase.zookeeper.quorum", "node101,node102,node103");
		Connection connection = ConnectionFactory.createConnection(conf) ;
		// 2.表
		Table table = connection.getTable(TableName.valueOf("student"));
		// 3.查询
		Get get = new Get(Bytes.toBytes("1001"));
		get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));
		get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"));
		// 4.执行
		Result result = table.get(get);
		// 5.查看  todo  Cell 表示HBase表中每列数据封装，包括 rowkey、columnfamily、column、value、ts时间戳等
		for (Cell cell : result.rawCells()) {
			String rowKey = Bytes.toString(CellUtil.cloneRow(cell));
			String cf = Bytes.toString(CellUtil.cloneFamily(cell));
			String column = Bytes.toString(CellUtil.cloneQualifier(cell));
			String value = Bytes.toString(CellUtil.cloneValue(cell));
			System.out.println(rowKey + "-> " +cf + ":" + column + " = " + value );
		}
		/*
			1001-> info:age = 25
			1001-> info:name = zhangsan
		 */
		// 6.关闭
		table.close();
		connection.close();
	}

}
